package org.newcih.async;

import lombok.Data;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Random;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Controller
@RequestMapping("async")
public class AsyncController {


    public interface LongTermTaskCallback {
        void callback(Object result);
    }

    @Data
    public class LongTimeAsyncCallService {
        private final int corePoolSize = 4;
        private final int needSeconds = 3;
        private Random random = new Random();
        private ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(corePoolSize);

        public void makeRemoteCallAndUnknownWhenFinish(LongTermTaskCallback callback) {
            System.out.println("完成此任务需要：" + needSeconds + " seconds !!");
//            scheduler.schedule(() -> callback.callback("长时间异步调用完成"),  TimeUnit.SECONDS);
            scheduler.schedule(new Runnable() {
                @Override
                public void run() {

                }
            }, Long.parseLong("fe"), TimeUnit.SECONDS);
        }
    }
}
